using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class SelectSpecialActionsToSpendDialog : Form
    {
        private int lineHeight = 0;
        private const int GAP = 10;
        private Dictionary<MapInfo.CountryID, HScrollBar> specialActionSelections;
        private Dictionary<MapInfo.CountryID, int> specialActionsAvailableToSpend;
        private Dictionary<HScrollBar, Label> labels;
        public SelectSpecialActionsToSpendDialog(Dictionary<MapInfo.CountryID, int> specialActionsAvailableToSpend)
        {
            int tabIndex = 0;
            lineHeight = GAP + EEGraphics.BritishSAs.Size.Height + GAP + 20;
            int countryScrollbarYPos = GAP + EEGraphics.BritishSAs.Size.Height + GAP;
            InitializeComponent();
            this.specialActionsAvailableToSpend = specialActionsAvailableToSpend;
            this.specialActionSelections = new Dictionary<MapInfo.CountryID, HScrollBar>();
            labels = new Dictionary<HScrollBar, Label>();
            foreach (MapInfo.CountryID country in specialActionsAvailableToSpend.Keys)
            {
                HScrollBar countryScrollBar = new HScrollBar();
                Label numSpecialActionsLabel = new System.Windows.Forms.Label();
                countryScrollBar.Location = new System.Drawing.Point(36, countryScrollbarYPos);
                countryScrollBar.Name = "hScrollBar" + Enum.GetName(country.GetType(), country);
                countryScrollBar.Size = new System.Drawing.Size(100, 20);
                countryScrollBar.TabIndex = tabIndex++;
                countryScrollBar.Minimum = 0;
                countryScrollBar.Maximum = specialActionsAvailableToSpend[country];
                countryScrollBar.SmallChange = 1;
                countryScrollBar.LargeChange = 1;
                countryScrollBar.ValueChanged += new EventHandler(countryScrollBar_ValueChanged);
                this.Controls.Add(countryScrollBar);
                specialActionSelections.Add(country, countryScrollBar);
                numSpecialActionsLabel.AutoSize = true;
                numSpecialActionsLabel.Font = new System.Drawing.Font("Century Gothic", 14.25F, System.Drawing.FontStyle.Bold);
                numSpecialActionsLabel.Location = new System.Drawing.Point(140, countryScrollbarYPos);
                numSpecialActionsLabel.Name = "numSpecialActions";
                numSpecialActionsLabel.Size = new System.Drawing.Size(21, 23);
                numSpecialActionsLabel.TabIndex = 37;
                numSpecialActionsLabel.Text = "0";
                this.Controls.Add(numSpecialActionsLabel);
                labels.Add(countryScrollBar, numSpecialActionsLabel);
                countryScrollbarYPos += lineHeight;
            }

            countryScrollbarYPos -= lineHeight;
            this.OkButton.Click += new EventHandler(OkButton_Click);
            this.OkButton.Location = new Point(this.OkButton.Location.X, countryScrollbarYPos + GAP + 20);
            this.Controls.Add(this.OkButton);
        }

        void OkButton_Click(object sender, EventArgs e)
        {
            bool foundSelection = false;
            foreach(MapInfo.CountryID country in this.specialActionSelections.Keys)
            {
                if (this.specialActionSelections[country].Value > 0)
                    foundSelection = true;
            }

            if(foundSelection)
            {
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "You must select at least one special action", "Reinforce Battle", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        void countryScrollBar_ValueChanged(object sender, EventArgs e)
        {
            labels[(HScrollBar)sender].Text = ((HScrollBar)sender).Value.ToString();
        }

        private void SelectSpecialActionsToSpendDialog_Paint(object sender, PaintEventArgs e)
        {
            int YPos = GAP;
            foreach (MapInfo.CountryID country in specialActionsAvailableToSpend.Keys)
            {
                PlayingPieceGraphics.PaintSpecialActions(e.Graphics, country, new Point(36 + EEGraphics.BritishSAs.Width / 2, YPos + EEGraphics.BritishSAs.Height / 2));
                YPos += lineHeight;
            }
        }

        public Dictionary<MapInfo.CountryID, int> GetSpecialActionsSelections()
        {
            Dictionary<MapInfo.CountryID, int> specialActionSelections = new Dictionary<MapInfo.CountryID,int>();
            foreach (MapInfo.CountryID country in this.specialActionSelections.Keys)
            {
                if (this.specialActionSelections[country].Value  > 0)
                    specialActionSelections.Add(country, this.specialActionSelections[country].Value);
            }

            return specialActionSelections;
        }
    }
}